﻿using hnrzxt.data;
using hnrzxt.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Model;
using NuGet.Protocol;

namespace hnrzxt.Controllers
{
    public class RccpsController : Controller
    {
        private readonly ApplicationDbContext _db;

        public RccpsController(ApplicationDbContext db)
        {
            _db = db;
        }

        public IActionResult Index()
        {
            //IEnumerable<Rccp> rccps = _db.Rccps;
            //return View(rccps);
            //var showrccp = _db.Rccps.Find(Jbxxid);
            // return View(showrccp);
            return View();
        }


        [HttpPost]
        public string Create(Rccp rccp)
        {
            if (ModelState.IsValid)
            {
                _db.Add(rccp);
                _db.SaveChanges();
            }
            else
            {
                return "插入失败";
            }
            return "";
        }

        public IActionResult asdf()
        {
            IEnumerable<Rccp> rccps = _db.Rccps;
            return View(rccps);
        }

        public Dictionary<string, object> ShowRccp(string Jbxxid)
        {
            Dictionary<string, object> map = new Dictionary<string, object>();
            map.Add("code", 0);
            map.Add("data", GetRccp(Jbxxid));


            return map;
        }

        public List<Dictionary<string, object>> GetRccp(string Jbxxid)
        {
            List<Rccp> rccplist = _db.Rccps.Where(P=>P.Jbxxid==Jbxxid).ToList();
            List<Dictionary<string, object>> list = new();
            for(int i = 0; i < rccplist.Count; i++)
            {
                Dictionary<string, object> map = new();
                map.Add("id",rccplist[i].Id);
                map.Add("CpTool", rccplist[i].CpTool);
                map.Add("CpType", rccplist[i].CpType);
                map.Add("DateTime", rccplist[i].DateTime.ToString("yyyy-MM-dd"));
                map.Add("CpResult", rccplist[i].CpResult);
                map.Add("CpFile", rccplist[i].CpFile);

                list.Add(map);
            }
            return list;
        }

        public Dictionary<string, object> ShowRccpbak(string Jbxxid)
        {
            List<Rccp> rccplist = _db.Rccps.Where(P => P.Jbxxid == Jbxxid).ToList();

            //List<hr_jcjl> li = _db.hr_jcjl.Where(P => P.uuid == Jbxxid).ToList();
            Dictionary<string, object> map = new();
            map.Add("code", 0);
            map.Add("data", rccplist);


            return map;


        }


        public string Del_rccp(int id)
        {
            try
            {
                var rccp = _db.Rccps.Find(id);
                if (rccp == null)
                {
                    return "数据不存在或已删除！";
                }
                _db.Rccps.Remove(rccp);
                _db.SaveChanges();
                return "删除成功！";
            }
            catch (Exception e)
            {
                return "删除失败:" + e.Message;
            }
        }

    }
}
